package indi.zhifa.study2024.core.mpenhence.utils;


import indi.zhifa.study2024.core.common.util.DtoEntityUtil;
import indi.zhifa.study2024.core.mpenhence.entity.po.SysBaseEntity;

public class DbDtoEntityUtil extends DtoEntityUtil {

    /**
     * 用于新增
     *
     * @param pDto   dto实体
     * @param clazz  po的class
     * @return 初始化好的po
     */
    public static <D, E> E createFromDto(D pDto, Class<E> clazz) {
        if (pDto == null) {
            return null;
        }
        E result = mapper.map(pDto, clazz);
        if(result instanceof SysBaseEntity){
            ((SysBaseEntity) result).createInit();
        }
        return result;
    }

    /**
     * 用于更新
     *
     * @param  pPo       待更新的po实体
     * @param  pDto      dto实体
     * @param  pPoClass  po的类
     * @return 更新后的po
     */
    public static <D, E> E editByDto(E pPo, D pDto, Class<E> pPoClass) {
        if (pDto == null) {
            return null;
        }
        E result = mapper.map(pPo, pPoClass);
        copy(result,pDto);
        if(result instanceof SysBaseEntity){
            ((SysBaseEntity) result).updateInit();
        }
        return result;
    }
}
